<?php


/**
 * this class provides some usefule function for handling persons
 * 
 * @author		simon ungerboeck <ungerboeck_simon@hotmail.com>
 * @version		1.0
 */

class Person {
	private $email;
	private $password;
	private $personId;
	private $personAttribute = array();
	private $sqlObj;


	/********************************************************************************
	 **************************** CONSTRUCTORS **************************************
	 *******************************************************************************/
	
	/**
	 * this constructor checks the arguments
	 * if 2 then initialize person by email and passwort
	 * if 1 then initialize person by personId
	 */
	public function __construct() {
		$this->sqlObj = Sql_Object::getInstance();
		
		$argv = func_get_args();
		switch (func_num_args()) {
			case 1 :
				self::__personById($argv[0]);
				break;
			case 2 :
				self::__personByMail($argv[0], $argv[1]);
				break;
		}
	}


	/**
	 * this constructor initializeses person by mail and passwort
	 * @param		string		$email
	 * @param		string		$passwort
	 */
	public function __personByMail($email, $password) {
		$this->email = $email;
		$this->password = $password;
		$sql = 'select * from ' . DB_CORE . '.person where email=? and password=?';
		$params = array($email, $password);
		$this->fillAttributes($sql, $params);
	}


	/**
	 * this constructor initializeses person by personId
	 * @param		string		$personId
	 */
	public function __personById($personId) {
		$this->personId = $personId;
		$sql = 'select * from ' . DB_CORE . '.person where person_id=?';
		$params = array($personId);
		$this->fillAttributes($sql, $params);
	}


	/********************************************************************************
	 **************************** END CONSTRUCTORS **************************************
	 *******************************************************************************/
	
	/********************************************************************************
	 **************************** PUBLIC FUNCTIONS***********************************
	 *******************************************************************************/
	
	/**
	 * checks if person exists in the system
	 * this function checks the cound of personAttribute. if greater than 0, person exists
	 * @param		string		$email
	 * @param		string		$password
	 * @return		bool		
	 */
	public function exists() {
		return ((count($this->personAttribute) > 0) ? true : false);
	}


	/**
	 * chesk if person exists in current
	 * @return		bool		$exists
	 */
	public function existsInApplication() {
		$exists = false;
		if ($this->exists()) {
				$sql = 'select * from ' . DB_CORE . '.application_person where person_id=? and application_id=? and active=?';
				$params = array($this->getAttribute('PERSON_ID'), APPLICATION_ID, ACTIVE);
				
				$sqlObj = Sql_Object::getInstance();
				$sqlObj->connect();
				$sqlObj->execute($sql, $params);
				$exists = ($sqlObj->getSelectRowCount() == 1) ? true : false;
				$sqlObj->disconnect();
		} 
		return $exists;
	}


	/**
	 * returns one attribute of person, of whole attribute list
	 * @param 		string				$key
	 * @return		string|array	
	 */
	public function getAttribute($key = null) {
		if ($key == null)
			return $this->personAttribute;
		else {
			if ($this->exists())
				return $this->personAttribute[strtoupper($key)];
		}
	}


	/********************************************************************************
	 **************************** END PUBLIC FUNCTIONS*******************************
	 *******************************************************************************/
	
	/********************************************************************************
	 **************************** PRIVATE FUNCTIONS**********************************
	 *******************************************************************************/
	
	/**
	 * fills the personAttribute array
	 * @param		string		$sql
	 * @param		array		$params
	 */
	private function fillAttributes($sql, $params) {
		$this->sqlObj->connect();
		$this->sqlObj->execute($sql, $params);
		if (($row = $this->sqlObj->fetch()) != null) {
			foreach ( $row as $attribute => $value )
				$this->personAttribute[strtoupper($attribute)] = $value;
		}
		$this->sqlObj->disconnect();
	}

/********************************************************************************
 **************************** END PRIVATE FUNCTIONS******************************
 *******************************************************************************/
}
?>